﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace Deflection
{
    public class Screen
    {
        public static SoundEffect MenuItemSelect { get; set; }
        public static SoundEffect StartGame { get; set; }
        public static SoundEffect Pause { get; set; }
        public List<MenuItem> Items { get; set; }
        public int CurrentItem { get; set; }

        public delegate void SelectionHandler(MenuItems selection);

        // The event we publish
        public event SelectionHandler Selection;

        // The method which fires the Event
        protected void OnSelection(MenuItems selection)
        {
            // Check if there are any Subscribers
            if (Selection != null)
            {
                // Call the Event
                Selection(selection);
            }
        }

        public enum ScreenState
        {
            AnimatingIn,
            AwaitingInput,
            AnimatingOut,

            OffScreen
        }
        protected ScreenState state { get; set; }
        public Screen()
        {
            CurrentItem = 0;
            state = ScreenState.OffScreen;
        }

        public virtual void Update(GameTime gameTime)
        {
            // Set all items to inactive
            Items.ForEach(delegate(MenuItem i)
            {
                i.Active = false;
            });

            // Check for key presses
            if (InputHandler.CheckKeyPress(Keys.W))
            {
                CurrentItem--;
                if (CurrentItem < 0)
                {
                    CurrentItem = 0;
                }
            }

            if (InputHandler.CheckKeyPress(Keys.S))
            {
                CurrentItem++;
                if (CurrentItem > Items.Count - 1)
                {
                    CurrentItem = Items.Count - 1;
                }
            }

            // Set the current item to active
            Items[CurrentItem].Active = true;


            if(InputHandler.CheckKeyPress(Keys.Enter))
            {
                Items[CurrentItem].Select();
            }

            Items.ForEach(delegate(MenuItem i)
            {
                i.Update(gameTime);
            });
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Items.ForEach(delegate(MenuItem i)
            {
                i.Draw(gameTime, spriteBatch);
            });
        }

        /// <summary>
        /// Override me
        /// </summary>
        /// <param name="index"></param>
        public virtual void MenuItemSelected(int index)
        {
            // Should be overrided for each screen
        }
    }
}
